package com.example.visitedsys.impl;

import com.example.visitedsys.Enum.UserTypeEnum;
import com.example.visitedsys.Shrio.Exception.ServiceException;
import com.example.visitedsys.converter.PartmentConverter;
import com.example.visitedsys.informationGet.PageVO;
import com.example.visitedsys.informationGet.PartVO;
import com.example.visitedsys.mapper.InterMapper;
import com.example.visitedsys.mapper.PartMapper;
import com.example.visitedsys.mapper.UserMapper;
import com.example.visitedsys.pojo.Inter;
import com.example.visitedsys.pojo.Part;
import com.example.visitedsys.pojo.User;
import com.example.visitedsys.service.PartService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 〈部门管理〉<br>
 * 〈〉
 * @author  SongPeng
 * @date  2021/11/04 14:24
 * @version 1.0
 */

@Service
public class PartServiceImpl implements PartService {
    @Autowired
    private PartMapper partMapper;
    @Autowired
    private InterMapper interMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PartVO edit(Long partId) {
        if(partMapper.selectByPrimaryKey(partId)==null){
            throw new ServiceException("编辑的部门不存在");
        }
        return PartmentConverter.converterToDepartmentVO(partMapper.selectByPrimaryKey(partId));
    }

    @Override
    public List<PartVO> findAllVO() {
        List<Part> parts = partMapper.selectAll();
            //转vo
            List<PartVO> partVOS = new ArrayList<>();
            if (!CollectionUtils.isEmpty(parts)) {
                for (Part part : parts) {
                    PartVO d = new PartVO();
                    BeanUtils.copyProperties(part, d);
                    Example o = new Example(Inter.class);
                    Example o2=new Example(User.class);
                    o2.createCriteria().andEqualTo("partId",part.getPartId());
                    o.createCriteria().andEqualTo("partId",part.getPartId());
                    d.setPartNumber((long) interMapper.selectCountByExample(o)+userMapper.selectCountByExample(o2));
                    partVOS.add(d);
                }
            }
            return partVOS;
        }
    /**
     * 部门列表
     *
     * @param pageNum
     * @param pageSize
     * @param partVO
     * @return
     */
    @Override
    public PageVO<PartVO> findpartList(Integer pageNum, Integer pageSize, PartVO partVO) {
        PageHelper.startPage(pageNum, pageSize);
        Example o = new Example(Part.class);
        if (partVO.getPartName() != null && !"".equals(partVO.getPartName())) {
            o.createCriteria().andLike("partName", "%" + partVO.getPartName() + "%");
        }
        List<Part> parts = partMapper.selectByExample(o);
        //转vo
        List<PartVO> departmentVOS = new ArrayList();
        if (!CollectionUtils.isEmpty(parts)) {
            for (Part part : parts) {
                PartVO d = new PartVO();
                BeanUtils.copyProperties(part, d);
                Example o1 = new Example(Inter.class);
                Example o2=new Example(User.class);
                o1.createCriteria().andEqualTo("partId",part.getPartId());
                o2.createCriteria().andEqualTo("partId",part.getPartId());
                d.setPartNumber((long) interMapper.selectCountByExample(o1)+userMapper.selectCountByExample(o2));
                departmentVOS.add(d);
            }
        }
        PageInfo<Part> info = new PageInfo<>(parts);
        return new PageVO<>(info.getTotal(), departmentVOS);
    }

    @Override
    public void add(PartVO partVO) {
        Part department = new Part();
        BeanUtils.copyProperties(partVO,department);
        department.setCreateTime(new Date());
        department.setModifiedTime(new Date());
        partMapper.insert(department);

    }

    @Override
    public void update(Long partId, PartVO partVO) {
        Part dbDepartment = partMapper.selectByPrimaryKey(partId);
        if(dbDepartment==null){
            throw new ServiceException("要更新的部门不存在");
        }
        Part department = new Part();
        BeanUtils.copyProperties(partVO,department);
        department.setPartId(partId);
        department.setModifiedTime(new Date());
        //根据主键更新属性不为null的值
        partMapper.updateByPrimaryKeySelective(department);


    }

    @Override
    public void delete(Long partId) {
        Part department = partMapper.selectByPrimaryKey(partId);
        if(department==null){
            throw new ServiceException("要删除的部门不存在");
        }
        //根据主键字段进行删除，方法参数必须包含完整的主键属性
        partMapper.deleteByPrimaryKey(partId);

    }
}
